
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Bill {
    String date;
    double amount;
    String category;
    String note;
    boolean isIncome;

    public Bill(String date, double amount, String category, String note, boolean isIncome) {
        this.date = date;
        this.amount = amount;
        this.category = category;
        this.note = note;
        this.isIncome = isIncome;
    }
}

public class test1 {
    public static void main(String[] args) {
        List<Bill> bills = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);

        double monthlyBudget = 0;
        while (true) {
            System.out.println("1. 记录收入");
            System.out.println("2. 记录支出");
            System.out.println("3. 查看所有账单");
            System.out.println("4. 查询账单");
            System.out.println("5. 预算管理");
            System.out.println("6. 月度统计");
            System.out.println("7. 退出");
            System.out.println("8. 年度总结");
            System.out.println("9. 查看最后一笔账单");
            System.out.print("请选择操作：");
            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    System.out.print("请输入收入日期：");
                    String incomeDate = scanner.nextLine();
                    System.out.print("请输入收入金额：");
                    double incomeAmount = scanner.nextDouble();
                    scanner.nextLine();
                    System.out.print("请输入收入类别：");
                    String incomeCategory = scanner.nextLine();
                    System.out.print("请输入备注：");
                    String incomeNote = scanner.nextLine();
                    bills.add(new Bill(incomeDate, incomeAmount, incomeCategory, incomeNote, true));
                    System.out.println("收入记录成功。");
                    break;
                case 2:
                    System.out.print("请输入支出日期：");
                    String expenseDate = scanner.nextLine();
                    System.out.print("请输入支出金额：");
                    double expenseAmount = scanner.nextDouble();
                    scanner.nextLine();
                    System.out.print("请输入支出类别：");
                    String expenseCategory = scanner.nextLine();
                    System.out.print("请输入备注：");
                    String expenseNote = scanner.nextLine();
                    bills.add(new Bill(expenseDate, expenseAmount, expenseCategory, expenseNote, false));
                    System.out.println("支出记录成功。");
                    break;
                case 3:
                    showAllBills(bills);
                    break;
                case 4:
                    queryBills(bills, scanner);
                    break;
                case 5:
                    System.out.print("请输入每月支出预算限额：");
                    monthlyBudget = scanner.nextDouble();
                    scanner.nextLine();
                    double totalExpenses = calculateTotalExpenses(bills);
                    double remainingBudget = monthlyBudget - totalExpenses;
                    System.out.println("当前剩余可用预算：" + remainingBudget);
                    break;
                case 6:
                    System.out.print("请输入要统计的年份和月份（格式：YYYY-MM）：");
                    String yearMonth = scanner.nextLine();
                    double totalIncome = calculateTotalIncomeForMonth(bills, yearMonth);
                    double totalExpense = calculateTotalExpensesForMonth(bills, yearMonth);
                    System.out.println("该月总收入：" + totalIncome);
                    System.out.println("该月总支出：" + totalExpense);
                    showCategoryAmountsForMonth(bills, yearMonth);
                    break;
                case 7:
                    System.out.println("退出系统。");
                    return;
                default:
                    System.out.println("无效选择，请重新输入。");
                    case 8:
    System.out.print("请输入要统计的年份（格式：YYYY）：");
    String year = scanner.nextLine();
    showAnnualSummary(bills, year);
    break;
                case 9:
                showLatestBill(bills);
                break;

            }
        }
    }

    private static void showAllBills(List<Bill> bills) {
        System.out.println("收入记录：");
        for (Bill bill : bills) {
            if (bill.isIncome) {
                System.out.println("日期：" + bill.date + ", 金额：" + bill.amount + ", 类别：" + bill.category + ", 备注：" + bill.note);
            }
        }
        System.out.println("支出记录：");
        for (Bill bill : bills) {
            if (!bill.isIncome) {
                System.out.println("日期：" + bill.date + ", 金额：" + bill.amount + ", 类别：" + bill.category + ", 备注：" + bill.note);
            }
        }
    }
    private static void queryBills(List<Bill> bills, Scanner scanner) {
        System.out.println("1. 按日期查询");
        System.out.println("2. 按类别查询");
        System.out.print("请选择查询方式：");
        int queryChoice = scanner.nextInt();
        scanner.nextLine();

        switch (queryChoice) {
            case 1:
                System.out.print("请输入查询日期：");
                String queryDate = scanner.nextLine();
                System.out.println("收入记录：");
                for (Bill bill : bills) {
                    if (bill.isIncome && bill.date.equals(queryDate)) {
                        System.out.println("日期：" + bill.date + ", 金额：" + bill.amount + ", 类别：" + bill.category + ", 备注：" + bill.note);
                    }
                }
                System.out.println("支出记录：");
                for (Bill bill : bills) {
                    if (!bill.isIncome && bill.date.equals(queryDate)) {
                        System.out.println("日期：" + bill.date + ", 金额：" + bill.amount + ", 类别：" + bill.category + ", 备注：" + bill.note);
                    }
                }
                break;
            case 2:
                System.out.print("请输入查询类别：");
                String queryCategory = scanner.nextLine();
                System.out.println("收入记录：");
                for (Bill bill : bills) {
                    if (bill.isIncome && bill.category.equals(queryCategory)) {
                        System.out.println("日期：" + bill.date + ", 金额：" + bill.amount + ", 类别：" + bill.category + ", 备注：" + bill.note);
                    }
                }
                System.out.println("支出记录：");
                for (Bill bill : bills) {
                    if (!bill.isIncome && bill.category.equals(queryCategory)) {
                        System.out.println("日期：" + bill.date + ", 金额：" + bill.amount + ", 类别：" + bill.category + ", 备注：" + bill.note);
                    }
                }
                break;
            default:
                System.out.println("无效选择。");
        }
    }

    private static double calculateTotalExpenses(List<Bill> bills) {
        double totalExpenses = 0;
        for (Bill bill : bills) {
            if (!bill.isIncome) {
                totalExpenses += bill.amount;
            }
        }
        return totalExpenses;
    }

    private static double calculateTotalIncomeForMonth(List<Bill> bills, String yearMonth) {
        double totalIncome = 0;
        for (Bill bill : bills) {
            if (bill.isIncome && bill.date.startsWith(yearMonth)) {
                totalIncome += bill.amount;
            }
        }
        return totalIncome;
    }

    private static double calculateTotalExpensesForMonth(List<Bill> bills, String yearMonth) {
        double totalExpense = 0;
        for (Bill bill : bills) {
            if (!bill.isIncome && bill.date.startsWith(yearMonth)) {
                totalExpense += bill.amount;
            }
        }
        return totalExpense;
    }

    private static void showCategoryAmountsForMonth(List<Bill> bills, String yearMonth) {
        List<String> categories = new ArrayList<>();
        for (Bill bill : bills) {
            if (bill.date.startsWith(yearMonth)) {
                if (bill.isIncome &&!categories.contains(bill.category)) {
                    categories.add(bill.category);
                } else if (!bill.isIncome &&!categories.contains(bill.category)) {
                    categories.add(bill.category);
                }
            }
        }
        System.out.println("各类别收入：");
        for (String category : categories) {
            double categoryIncome = 0;
            for (Bill bill : bills) {
                if (bill.isIncome && bill.category.equals(category) && bill.date.startsWith(yearMonth)) {
                    categoryIncome += bill.amount;
                }
            }
            System.out.println(category + ": " + categoryIncome);
        }
        System.out.println("各类别支出：");
        for (String category : categories) {
            double categoryExpense = 0;
            for (Bill bill : bills) {
                if (!bill.isIncome && bill.category.equals(category) && bill.date.startsWith(yearMonth)) {
                    categoryExpense += bill.amount;
                }
            }
            System.out.println(category + ": " + categoryExpense);
        }
    }
private static void showAnnualSummary(List<Bill> bills, String year) {
    double totalIncome = 0;
    double totalExpense = 0;
    List<String> categories = new ArrayList<>();

    // 计算年度总收入和总支出
    for (Bill bill : bills) {
        if (bill.date.startsWith(year)) {
            if (bill.isIncome) {
                totalIncome += bill.amount;
            } else {
                totalExpense += bill.amount;
            }
            if (!categories.contains(bill.category)) {
                categories.add(bill.category);
            }
        }
    }
    
    System.out.println("年度总结 (" + year + "年):");
    System.out.println("总收入：" + totalIncome);
    System.out.println("总支出：" + totalExpense);
    System.out.println("按类别汇总：");
    
    // 按类别显示收入和支出
    for (String category : categories) {
        double categoryIncome = 0;
        double categoryExpense = 0;
        for (Bill bill : bills) {
            if (bill.date.startsWith(year) && bill.category.equals(category)) {
                if (bill.isIncome) {
                    categoryIncome += bill.amount;
                } else {
                    categoryExpense += bill.amount;
                }
            }
        }
        System.out.println(category + " - 收入: " + categoryIncome + ", 支出: " + categoryExpense);
    }
}

private static void showLatestBill(List<Bill> bills) {
    if (bills.isEmpty()) {
        System.out.println("目前没有账单记录。");
        return;
    }
    
    Bill latestBill = bills.get(bills.size() - 1); // 获取最后一条记录
    if (latestBill.isIncome) {
        System.out.println("最近一笔收入记录：");
    } else {
        System.out.println("最近一笔支出记录：");
    }
    System.out.println("日期：" + latestBill.date);
    System.out.println("金额：" + latestBill.amount);
    System.out.println("类别：" + latestBill.category);
    System.out.println("备注：" + latestBill.note);
}

}

  